package ai.fabu.dataplatform.service.impl;

import ai.fabu.dataplatform.bean.bagdata.BagData;
import ai.fabu.dataplatform.bean.util.IndexData;
import ai.fabu.dataplatform.constant.ResultCode;
import ai.fabu.dataplatform.help.Result;
import ai.fabu.dataplatform.service.ApiForDeprecationService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoOperations;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;

import javax.validation.constraints.NotNull;

import static ai.fabu.dataplatform.constant.ConstantName.BAG_NAME;
import static ai.fabu.dataplatform.constant.ConstantName.GROUP_NAME;

@Slf4j
@Service
public class ApiForDeprecationServiceImpl implements ApiForDeprecationService {
    @Autowired
    MongoOperations mongoOperations;

    @Deprecated
    @Override
    public Result<IndexData> getGroupNameByBagName(@NotNull String bagName) {
        String methodName = new Exception().getStackTrace()[0].getMethodName();
        log.info("{}: bag name: {}", methodName, bagName);
        Result<IndexData> result = new Result<>();
        if(bagName.isEmpty()){
            log.error("{}: bag name is empty", methodName);
            result.returnWithoutValue(ResultCode.LACK_PARAM_ERROR);
        }
        Query query = new Query();
        query.addCriteria(Criteria.where(BAG_NAME).is(bagName));
        query.fields().include(GROUP_NAME);
        BagData bagData = mongoOperations.findOne(query, BagData.class);
        if(bagData == null){
            log.error("{}: bag data is null", methodName);
            result.returnWithoutValue(ResultCode.NO_DATA);
            return result;
        }
        IndexData indexData = new IndexData();
        indexData.setGroupName(bagData.getGroupName());
        indexData.setBagName(bagName);
        result.returnWithValue(ResultCode.SUCCESS, indexData);
        return result;
    }
}
